﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Babelfish.Javascript;
using Babelfish.MetaData;
using Babelfish.Node;

namespace Babelfish.JS
{
    /// <summary>
    /// 
    /// </summary>
    public class JsNode : GeneralNode, INode
    {
        /// <summary>
        /// Gets or sets the js object.
        /// </summary>
        /// <value>The js object.</value>
        protected JsObject JsObject { get; set; }

        /// <summary>
        /// Removes this this node from its parent node.
        /// </summary>
        public void Remove(INode childNode)
        {

        }

        #region INode Members

        /// <summary>
        /// Gets the name of this node.
        /// </summary>
        /// <value>The name.</value>
        public string Name
        {
            get { return JsObject.Name; }
        }

        /// <summary>
        /// Gets the value of this node.
        /// </summary>
        /// <value>The value.</value>
        public string Text
        {
            get { return JsObject.Value; }
        }

        /// <summary>
        /// Gets the metadata of this node, like html or xml comments
        /// </summary>
        /// <value></value>
        public MetaDataCollection MetaData
        {
            get 
            {
                var metaData = new MetaDataCollection();

                foreach (var child in JsObject.Children)
                    if (child.Type == JsObjectType.Comment)
                        metaData.Add(new GeneralMetaData { Name = child.Name, Value = child.Value });

                return metaData;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this node has a value.
        /// </summary>
        /// <value><c>true</c> if this node has a value; otherwise, <c>false</c>.</value>
        public bool HasValue
        {
            get { return JsObject.Value != null; }
        }

        /// <summary>
        /// Gets the parent node of this node.
        /// </summary>
        /// <value>The parent node.</value>
        public INode Parent
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the siblings of this node.
        /// </summary>
        /// <value>The sibling nodes.</value>
        public NodeCollection Siblings
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets the child nodes of this node.
        /// </summary>
        /// <value>The child nodes.</value>
        public NodeCollection ChildNodes
        {
            get 
            {
                var collection = new NodeCollection();

                foreach (var child in JsObject.Children)
                    if (child.Type != JsObjectType.Comment)
                        collection.Add(new JsNode{ Parent = this, JsObject = child });

                return collection;
            }
        }

        private AttributeCollection _attributes;
        /// <summary>
        /// Gets the attributes of this node.
        /// </summary>
        /// <value>The attributes.</value>
        public AttributeCollection Attributes
        {
            get 
            {
                if (_attributes != null)
                    return _attributes;

                _attributes = new AttributeCollection();
                _attributes.Add(new GeneralAttribute { Name = "type", Value = this.JsObject.Type.ToString() });


                return _attributes;
            }
        }

        #endregion
    }
}
